#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>
#include <vector>

using namespace std;

#define NUM 3

struct ThreadInfo
{
    string threadname;
};

string ToHex(pthread_t tid)
{
    char buffer[64];
    snprintf(buffer, sizeof(buffer), "%ld", tid);
    return buffer;
}

// void* ThreadRoutie(void* args)
// {
//     ThreadInfo* ti = static_cast<ThreadInfo*>(args);
//     for(int i = 0; i < 10; i++)
//     {
//         cout << ti->threadname.c_str() << " is running, tid: " << ToHex(pthread_self()) << ", pid: " << getpid() << endl;
//     }
//     return nullptr;
// }

void *ThreadRoutine(void *args)
{
    int i = 0;
    ThreadInfo *ti = static_cast<ThreadInfo*>(args);
    while(i < 10)
    {
        cout << ti->threadname.c_str() << " is running, tid: " << ToHex(pthread_self()) << ", pid: " << getpid() << endl;
        i++;
        sleep(1);
    }

    return nullptr;
}

int main()
{
    vector<pthread_t> tids;
    for(int i = 0; i < NUM; i++)
    {
        pthread_t tid;
        ThreadInfo* id = new ThreadInfo;
        id->threadname = "Thread-" + to_string(i);
        pthread_create(&tid, nullptr, ThreadRoutine, id);

        tids.push_back(tid);
        sleep(1);
    }

    for(auto tid: tids)
    {
        pthread_join(tid, nullptr);
    }

    return 0;
}


